<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Menus and Toolbars in PyQt4</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="PyQt, PyQt4, menus, toolbars, tutorial, Python, Linux">
<meta name="description" content="In this part of the PyQt4 tutorial, we work
with menus and toolbars.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href=".." title="Home">Contents</a>


<h1>Menus and Toolbars in PyQt4</h1>

<p>
In this part of the PyQt4 tutorial, we will create menus and toolbars.
A menu is a group of commands located in a menubar. A toolbar has buttons
with the common commands in the application.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 


<h2>Main Window</h2>

<p>
The <code>QtGui.QMainWindow</code> class provides a main application 
window. This enables to create the classic application
skeleton with a statusbar, toolbars and a menubar.
</p>

<h2>Statusbar</h2>

<p>
The statusbar is a widget that is used for displaying status information.
</p>

<pre class="code">
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

This program creates a statusbar.

author: Jan Bodnar
website: zetcode.com 
last edited: September 2011
"""

import sys
from PyQt4 import QtGui


class Example(QtGui.QMainWindow):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):               
        
        self.statusBar().showMessage('Ready')
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Statusbar')    
        self.show()


def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
</pre>

<p>
A statusbar is created with the help of the <code>QtGui.QMainWindow</code> widget.
</p>

<pre class="explanation">
self.statusBar().showMessage('Ready')
</pre>

<p> 
To get the statusbar, we call the <code>statusBar()</code> method 
of the <code>QtGui.QMainWindow</code> class. The first call of the method
creates a status bar. Subsequent calls return the statusbar object. 
The <code>showMessage()</code> displays a message on the statusbar.
</p> 


<h2>Menubar</h2>

<p>
A menubar is a common part of a GUI application. It is a group of commands 
located in various menus. While in console applications we have to remember 
various commands and their options, here we have most of the commands grouped 
into logical parts. These are accepted standards that further reduce the amount 
of time spending to learn a new application. 
</p>

<pre class="code">
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

This program creates a menubar. The
menubar has one menu with an exit action.

author: Jan Bodnar
website: zetcode.com 
last edited: August 2011
"""

import sys
from PyQt4 import QtGui

class Example(QtGui.QMainWindow):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):               
        
        exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&amp;Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(QtGui.qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&amp;File')
        fileMenu.addAction(exitAction)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Menubar')    
        self.show()
        
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()    
</pre>

<p>
In the above example, we create a menubar with one menu. 
This menu will contain one action, which will terminate the 
application if selected. A statusbar is created as well. 
The action is accessible with the Ctrl + Q shortcut.
</p>

<pre class="explanation">
exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&amp;Exit', self)        
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
</pre>

<p> 
A  <code>QtGui.QAction</code> is an abstraction for actions
performed with a menubar, toolbar or with a custom keyboard shortcut. 
In the above three lines, we create an action, with a specific icon and
an 'Exit' label. Furthermore, a shortcut is defined for this action. 
The third line creates a status tip, which is shown in the statusbar, when
we hover a mouse pointer over the menu item. 
</p> 

<pre class="explanation">
exitAction.triggered.connect(QtGui.qApp.quit)
</pre>

<p> 
When we select this particular action, a triggered signal is emitted. 
The signal is connected to the <code>quit()</code> method of the
<code>QtGui.QApplication</code> widget. This terminates the application.
</p> 

<pre class="explanation">
menubar = self.menuBar()
fileMenu = menubar.addMenu('&amp;File')
fileMenu.addAction(exitAction)
</pre>

<p> 
The <code>menuBar()</code> method creates a menubar. We create a 
file menu and append the exit action to it. 
</p> 


<h2>Toolbar</h2>

<p>
Menus group all commands that we can use in an application. 
Toolbars provide a quick access to the most frequently used commands.
</p>

<pre class="code">
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

This program creates a toolbar.
The toolbar has one action, which
terminates the application, if triggered.

author: Jan Bodnar
website: zetcode.com 
last edited: September 2011
"""

import sys
from PyQt4 import QtGui

class Example(QtGui.QMainWindow):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):               
        
        exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(QtGui.qApp.quit)
        
        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Toolbar')    
        self.show()
        
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
</pre>

<p>
In the above example, we create a simple toolbar. The toolbar has one 
tool action. An exit action, which terminates the application, when triggered.
</p>

<pre class="explanation">
exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(QtGui.qApp.quit)
</pre>

<p> 
Similar to the menubar example above, we create an action object. 
The object has a label, icon and a shorcut. A <code>quit()</code> method of the
<code>QtGui.QMainWindow</code> is connected to the triggered signal.
</p> 


<pre class="explanation">
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)
</pre>

<p>
Here we create a toolbar and plug and action object into it. 
</p>

<br>
<img src="/img/gui/pyqt4/toolbar.png" alt="Toolbar">
<div class="figure">Figure: Toolbar</div>


<h2>Putting it together</h2>

<p>
In the last example of this section, we will create a menubar, toolbar 
and a statusbar. We will also create a central widget.
</p>

<pre class="code">
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

This program creates a skeleton of
a classic GUI application with a menubar,
toolbar, statusbar and a central widget. 

author: Jan Bodnar
website: zetcode.com 
last edited: September 2011
"""

import sys
from PyQt4 import QtGui

class Example(QtGui.QMainWindow):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):               
        
        textEdit = QtGui.QTextEdit()
        self.setCentralWidget(textEdit)

        exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(self.close)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&amp;File')
        fileMenu.addAction(exitAction)

        toolbar = self.addToolBar('Exit')
        toolbar.addAction(exitAction)
        
        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle('Main window')    
        self.show()
        
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()    
</pre>

<p> 
This code example creates a skeleton of a classic GUI application
with a menubar, toolbar and a statusbar. 
</p> 

<pre class="explanation">
textEdit = QtGui.QTextEdit()
self.setCentralWidget(textEdit)
</pre> 
 
<p> 
Here we create a text edit widget. We set it to be the central widget 
of the <code>QtGui.QMainWindow</code>. The central widget will 
occupy all space that is left.
</p> 

<br>
<img src="/img/gui/pyqt4/mainwindow.png" alt="MainWindow">
<div class="figure">Figure: MainWindow</div>

<hr class="btm">

<p>
In this part of the PyQt4 tutorial, we worked with menus, toolbars, statusbar 
and a main application window.
</p>

<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified September 18, 2011  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

